package com.situ.day4;

import java.util.Scanner;

import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

public class StringDemo {

	@Test
	public void test1() {
		String str = "java AndroidA";
		System.out.println(str.length());
		char[] array = str.toCharArray();
		char ch = str.charAt(5);
		System.out.println(ch);// A
		System.out.println(str.indexOf('A'));//5
		//如果找不到返回-1
		System.out.println(str.indexOf('X'));//-1
		System.out.println(str.indexOf('A', 6));//12
		System.out.println(str.indexOf("And"));//5
		
		System.out.println(str.lastIndexOf('A'));//12
	}
	
	@Test
	public void test2() {
		String str = "java AndroidA";
		String str1 = "java Androida";
		System.out.println(str.equals("java AndroidA"));//true
		System.out.println(str.equalsIgnoreCase(str1));//true
		System.out.println(str.contains("And"));//true
		System.out.println(str.startsWith("java A"));//true
		System.out.println(str.startsWith("And", 5));//true
		System.out.println(str.endsWith("oidA"));//true
		System.out.println(str.isEmpty());//false
		System.out.println(" ".isEmpty());//false
		System.out.println("".isEmpty());//ture
	}
	
	@Test
	public void test3() {
		//String str = "java And";
		char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
		String str = new String(array);
		System.out.println(str);//"java And"
		char[] charArray = str.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			System.out.println(charArray[i]);
		}
	}
	
	@Test
	public void test4() {
		String str = "java AndroidA";
		String newStr = str.replace('A', 'B');
		System.out.println(str);
		System.out.println(newStr);
	}
	
	@Test
	public void test5() {
		String str = "java Android Python";
		String[] array = str.split(" ");
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test6() {
		String str = "java Android";
		System.out.println(str.substring(5));//Android
		System.out.println(str.substring(5, 8));//And
	}
	
	@Test
	public void test7() {
		String str = "   java Android   ";
		System.out.println(str);
		System.out.println(str.trim());
	}
	
	@Test
	public void test8() {
		String str1 = "Hello";
		String str2 = "Java";
		String str3 = "Android";
		String str4 = "Python";
		//String长度是不可改变的,每次变量拼接都会new一个新的String对象
		String str = str1 + str2 + str3 + str4;
		
		StringBuilder builder = new StringBuilder(str1);//Hello
		builder.append(str2);//HelloJava
		builder.append(str3);//HelloJavaAndroid
		builder.append(str4);//HelloJavaAndroidPython
		System.out.println(builder);
		
		//链式编程
		StringBuffer buffer = new StringBuffer(str1);
		buffer.append(str2).append(str3).append(str4);
	}
	
	@Test
	public void test55() {
		String string = Boolean.toString(true);//"true"
		System.out.println(string);
		String str = "---java Android---";
		str.trim();
	}
	
	@Test
	public void test57() {
		// "Java"
		// str.charAt(4)
		String str = "     ";
		String result = trim(str);
	}

	private String trim(String str) {
		int startIndex = 0;
		int endIndex = str.length() - 1;
		while ((startIndex <= endIndex) && (str.charAt(startIndex) == ' ')) {
			startIndex++;
		}
		while ((startIndex <= endIndex) && (str.charAt(endIndex) == ' ')) {
			endIndex--;
		}
		System.out.println(startIndex);
		System.out.println(endIndex);
		String result = str.substring(startIndex, endIndex + 1);
		System.out.println("");
		return result;
	}
	
	@Test
	public void test22() {
		System.out.println("请输入：");
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
//		 直接调用方法转换为小写
//		System.out.println(str.toLowerCase());
		
//		通过转化成字符数组然后遍历转化
		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if (array[i] >= 'A' && array[i] <= 'Z') {
				array[i] += 32;
			}
		}
		String newstr = new String(array);
		System.out.println(newstr);
		scanner.close();
	}

	
	@Test
	public void test44() {
		String str = "this is my homework! I must finish it!";
		String[] array = str.split(" ");
		System.out.println(array.length);
		
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			count++;
		}
		System.out.println("单词个数：" + count);
	}
	
	
}
